import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Main from '../components/main.vue'
import Sign from '../components/sign.vue'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    
  },
  {
    path:'/main',
    name:'main',
    meta: {
      requireAuth: true // 配置此条，进入页面前判断是否需要登陆
    },
    component: Main,
    children:[
      {
        path: 'content',
        name: 'Content',
        // redirect: { path: '/' },
        component: () => import('../components/Content.vue')
      },
      {
        path: 'echarts',
        name: 'Echarts',
        // redirect: { path: '/' },
        component: () => import('../components/Echats.vue')
      }
    ]
  },
  {
    path:'/Sign',
    name:'sign',
    component: Sign
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// router.beforeEach((to,from,next)=>{
//   // if(to.path==='/Home') return next()
//   //获取token
//   const tokenStr=window.localStorage.getItem('token')
//   if(tokenStr) return next('/Main')
// })

export default router

router.beforeEach((to, from, next) => {
  // console.log('to, from :>> ', to, from);
  console.log('to :>> ', to);
  console.log('from :>> ', from);
  const { path: fromPath } = from;
  const username = window.localStorage.getItem('user');
  
  console.log('username :>> ', username);
  if (username) {
    router.push('/');
  }
  if (to.name !== '/' && !true) next({ name: 'main' })
  else next()
})

// router.beforeEach((to, from, next) => {
//   let islogin = sessionStorage.getItem("token");
//   islogin = Boolean(Number(islogin));
 
//   if(to.path == "/"){
//     if(islogin){
//       next('/main');
//     }else{
//       next();
//     }
//   }

// })

